# sftp_logic.py
import paramiko

class SFTPManager:
    def __init__(self, host, username, password):
        self.host = host
        self.username = username
        self.password = password
        self.ssh_client = None

    def connect(self):
        try:
            self.ssh_client = paramiko.SSHClient()
            self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh_client.connect(self.host, username=self.username, password=self.password)
            print(f"Connected to {self.host} via SSH")
        except Exception as e:
            print(f"Error connecting to {self.host}: {e}")
            raise

    def transfer_command(self, command):
        try:
            stdin, stdout, stderr = self.ssh_client.exec_command(command)
            result = stdout.read().decode('utf-8')
            print(result)
            return result
        except Exception as e:
            print(f"Error executing command: {e}")
            return str(e)

    def disconnect(self):
        if self.ssh_client is not None:
            self.ssh_client.close()
            print(f"Disconnected from {self.host}")

    def is_connected(self):
        return self.ssh_client is not None and self.ssh_client.get_transport().is_active()

